package com.example.boxuegu;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.media.Image;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView tv_back;
    private TextView tv_main_title;
    private RelativeLayout title_bar;
    private LinearLayout main_bottom_bar;
    private View main_main;
    private View main_xt;
    private View main_me;
    private TextView main_text_sp;
    private TextView main_text_xt;
    private TextView main_text_me;
    private ImageView main_image_sp;
    private ImageView main_image_xt;
    private ImageView main_image_me;
    private FrameLayout mBodylayout;
    private myinfo myinfo1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置此界面为竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        initBottomBar();
        setListener();//监听底部三个按钮
        setInitStatus();//初始化界面View
    }

    private void setInitStatus() {
        clearBottomImageState();
        setSelectedStatus(0);
        createView(0);
    }



    /*
     * 获取界面上的UI控件*/
    private void init() {
        tv_back = findViewById(R.id.tv_back);
        tv_main_title = findViewById(R.id.tv_main_title);//标题
        tv_main_title.setText("博学谷课程");
        title_bar = findViewById(R.id.title_bar);
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
        tv_back.setVisibility(View.GONE);
        initBodyLayout();
    }

    private void initBodyLayout() {
        mBodylayout = findViewById(R.id.main_body);
    }
    /*
     * 获取底部控件*/

    private void initBottomBar() {
        main_bottom_bar = findViewById(R.id.main_bottom_bar);
        main_main = findViewById(R.id.main_mian);//视频
        main_xt = findViewById(R.id.main_xt);//习题
        main_me = findViewById(R.id.main_me);//我的
        //视频文字
        main_text_sp = findViewById(R.id.mian_text_sp1);
        //习题文字
        main_text_xt = findViewById(R.id.main_text_xt2);
        //我的文字
        main_text_me = findViewById(R.id.main_text_me2);
        /*
         * 图片*/
        //视频图片
        main_image_sp = findViewById(R.id.mian_image_sp);
        //习题图片
        main_image_xt = findViewById(R.id.mian_image_xt1);
        //我的图片
        main_image_me = findViewById(R.id.main_image_me1);
    }


    /*
     * 点击事件*/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //课程点击事件
            case R.id.main_mian:
                clearBottomImageState();
                selectDisplayView(0);
                break;
            case R.id.main_xt:
                clearBottomImageState();
                selectDisplayView(1);
                break;
            case R.id.main_me:
                clearBottomImageState();
                selectDisplayView(2);
                break;
            default:
                break;

        }

    }
    /*
     * 设置底部三个按钮的监听事件*/
    private void setListener() {
        for (int i=0;i<main_bottom_bar.getChildCount();i++){
            main_bottom_bar.getChildAt(i).setOnClickListener(this);
        }
    }
    /*

     * 清除底部按钮控件*/

    private void clearBottomImageState() {
        //清除文字的状态
        main_text_sp.setTextColor(Color.parseColor("#666666"));
        main_text_xt.setTextColor(Color.parseColor("#666666"));
        main_text_me.setTextColor(Color.parseColor("#666666"));
        //清除图片的状态
        main_image_sp.setImageResource(R.drawable.main_course_icon);
        main_image_xt.setImageResource(R.drawable.main_exercises_icon);
        main_image_me.setImageResource(R.drawable.main_my_icon);
        for (int i = 0; i < mBodylayout.getChildCount(); i++) {
            mBodylayout.getChildAt(i).setSelected(false);

        }
    }
    /*
     * 点击后显示对应的界面*/

    private void selectDisplayView(int i) {
        removeAllView();//移除不需要的视图
        createView(i);
        setSelectedStatus(i);//按钮选中状态
    }

    private void removeAllView() {
        for(int i=0;i<mBodylayout.getChildCount();i++){
            mBodylayout.getChildAt(i).setVisibility(View.GONE);
        }
    }

    /*
    * 选择视图*/
    private void createView(int intext) {
        switch (intext){
            case 0:
                break;
            case 1:
                break;
            case 2:
                if(myinfo1==null){
                    myinfo1=new myinfo(this);
                    mBodylayout.addView(myinfo1.getView());

                }else {
                    myinfo1.getView();

                }
                myinfo1.showView();
                break;
        }
    }


    /*
     * 按钮的选中状态*/
    private void setSelectedStatus(int indext) {
        switch (indext) {
            case 0:
                main_main.setSelected(true);//设置选择
                main_image_sp.setImageResource(R.drawable.main_course_icon_selected);
                main_text_sp.setTextColor(Color.parseColor("#0097f7"));
                title_bar.setVisibility(View.VISIBLE);//设置标题可见
                tv_main_title.setText("课程");
                break;
            case 1:
                main_xt.setSelected(true);
                main_image_xt.setImageResource(R.drawable.main_exercises_icon_selected);
                main_text_xt.setTextColor(Color.parseColor("#0097f7"));
                title_bar.setVisibility(View.VISIBLE);//设置标题可见
                tv_main_title.setText("习题");
                break;
            case 2:
                main_me.setSelected(true);
                main_image_me.setImageResource(R.drawable.main_my_icon_selected);
                main_text_me.setTextColor(Color.parseColor("#0097f7"));
                title_bar.setVisibility(View.VISIBLE);//设置标题可见
                tv_main_title.setText("我的");
                break;
        }
    }
    protected long extime;//记录第一次点击的时间

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-extime)>2000){
                Toast.makeText(this, "再点击一次退出博学谷", Toast.LENGTH_SHORT).show();
                extime=System.currentTimeMillis();
            }else{
                MainActivity.this.finish();
                if(readLoginStatus()){
                    clearLoginStatus();
                }
            System.exit(0);
        }return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void clearLoginStatus() {
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=loginInfo.edit();//获取编辑器
        editor.putBoolean("isLogin",false);
        editor.putString("loginUserName","");
        editor.commit();
    }

    private boolean readLoginStatus() {
        SharedPreferences loginInfo = getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
        boolean isLogin = loginInfo.getBoolean("isLogin", false);
        return isLogin;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            boolean isLogins = data.getBooleanExtra("isLogin", false);
            if(isLogins){//登陆成功后显示课程界面
                clearBottomImageState();
                selectDisplayView(0);

            }
            if(myinfo1!=null){
                myinfo1.setLogiinParams(isLogins);
            }
        }
    }
}